/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.zjdt.epserver.db.dao4mysql;

import com.zjdt.epserver.db.dao.UserDao;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 *
 * @author dusin
 */
public class UserDaoImpl implements UserDao{

    static Logger logger = Logger.getLogger(UserDaoImpl.class);
    private JdbcTemplate jdbcTemplate;

    public UserDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }   
    
    
    @Override
    public boolean doCreate(Object[] bean) {
        boolean flag = true;
        String sql = null;
        sql = "insert into user(username,password,name,mobilePhone,note) values(?,?,?,?,?)";
        int rows = jdbcTemplate.update(sql, bean);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doDeleteById(int id) {
        boolean flag = true;
        String sql = null;
        sql = "delete from user where id=?";
        int rows = jdbcTemplate.update(sql, id);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doUpdateById(Object[] bean) {
        boolean flag = true;
        String sql = null;
        sql = "update user set username=?,name=?,mobilePhone=?,note=? where id=?";
        int rows = jdbcTemplate.update(sql, bean);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doUpdatePassword(int id, String password) {
        boolean flag = true;
        String sql = null;
        sql = "update user set password=? where id=?";
        int rows = jdbcTemplate.update(sql, password,id);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }
    
    @Override
    public boolean doUpdatePasswordByUsername(String username, String password) {
        boolean flag = true;
        String sql = null;
        sql = "update user set password=? where username=?";
        int rows = jdbcTemplate.update(sql, password,username);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }
    @Override
    public boolean doUpdateMobile(int id, String phoneNumber) {
        boolean flag = true;
        String sql = null;
        sql = "update user set mobilePhone=? where id=?";
        int rows = jdbcTemplate.update(sql, phoneNumber,id);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }
    
    @Override
    public Map<String, Object> findById(int id) {
        String sql = null;
        try{
            sql = "select * from  user where id = ?";
            return jdbcTemplate.queryForMap(sql,id);        
        }catch(EmptyResultDataAccessException e){
            return null;
        }
        
    }

    @Override
    public List<Map<String, Object>> findAll() {        
        String sql = "select * from user";
        return jdbcTemplate.queryForList(sql);
    }

    @Override
    public Map<String, Object> login(String username, String password) {
        String sql;        
        sql = "select * from  user where username = ?  and password=?";
        Map<String, Object> user=null;
        try{
            user = jdbcTemplate.queryForMap(sql,username,password);
        }catch(Exception e){            
        }
        return user; 
    }    
    
    //

    @Override
    public List<Map<String, Object>> findAllModuleByUsername(String username) {
        String sql;        
        sql = "select id,no,name,url,type,iconcls,note,menuCode,superCode,status from module where menuCode in (select menuCode from usermodule where username=?)";
        List<Map<String, Object>> modules=null;
        try{
            modules = jdbcTemplate.queryForList(sql,username);
        }catch(Exception e){            
        }
        return modules; 
    }

    @Override
    public boolean doDeleteAllModuleByUsername(String username) {
         boolean flag = true;
        String sql = null;
        sql = "delete from usermodule where username=?";
        int rows = jdbcTemplate.update(sql, username);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doCreateAllModuleByUsername(String username, String[] menuCodes) {
        boolean flag = true;
        String sql = null;
        sql = "insert into usermodule(username,menuCode) values(?,?)";
        List<Object[]> beans = new ArrayList<Object[]>();
        for(String code:menuCodes){
            beans.add(new Object[]{username,code});
        }
        int[] rows = jdbcTemplate.batchUpdate(sql, beans);
        if (rows == null) {
            flag = false;
        }
        return flag;
    }    
}
